package com.comm.utils.excel;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * poi工具类
 */
public class PoiUtils {

    /**
     * 构建wb示例
     *
     * public HSSFWorkbook fieldDetailExcel(List<FieldDetailVO> data){
     *
         // excel 基础信息构建
         HSSFWorkbook workbook = new HSSFWorkbook();
         HSSFSheet sheet = workbook.createSheet();
         HSSFRow row = null;
         int rowNum = 1;

         row = sheet.createRow(0);
         row.createCell(0).setCellValue("票据编号");
         row.createCell(1).setCellValue("模板编号");
         row.createCell(2).setCellValue("模板名称");
         row.createCell(3).setCellValue("字段名称");
         row.createCell(4).setCellValue("录入类型");
         row.createCell(5).setCellValue("人员姓名");
         row.createCell(6).setCellValue("提交时间");
         row.createCell(7).setCellValue("录入字节数");
         row.createCell(8).setCellValue("录入值");
         row.createCell(9).setCellValue("正确值");
         row.createCell(10).setCellValue("录入异常值");
         row.createCell(11).setCellValue("正确异常值");
         row.createCell(12).setCellValue("影像地址");

         FieldDetailVO detailVO = null;

         for(int i = 0; i < data.size(); i++){
         detailVO = data.get(i);
         row = sheet.createRow(rowNum);
         row.createCell(0).setCellValue(strIsEmpty(detailVO.getBillCode()));
         row.createCell(1).setCellValue(strIsEmpty(detailVO.getTemplateNo()));
         row.createCell(2).setCellValue(strIsEmpty(detailVO.getTemplateName()));
         row.createCell(3).setCellValue(strIsEmpty(detailVO.getTemplateFieldName()));
         row.createCell(4).setCellValue(StringUtils.isNotEmpty(detailVO.getTaskCategory()) ?
         TaskTaskCategoryEnum.toMap().get(detailVO.getTaskCategory()) : "");
         row.createCell(5).setCellValue(strIsEmpty(detailVO.getApplicantName()));
         row.createCell(6).setCellValue(detailVO.getSubmitTime() == null ? "" :
         DateUtils.formatDate(detailVO.getSubmitTime(), DateUtils.yyyyMMdd_HH_mm_ss));// 转换时间
         row.createCell(7).setCellValue(strIsEmpty(detailVO.getSubmitBytes()));
         row.createCell(8).setCellValue(strIsEmpty(detailVO.getSubmitValue()));
         row.createCell(9).setCellValue(strIsEmpty(detailVO.getRightValue()));
         row.createCell(10).setCellValue(strIsEmpty(detailVO.getSubmitException()));
         row.createCell(11).setCellValue(strIsEmpty(detailVO.getRightException()));
         row.createCell(12).setCellValue(strIsEmpty(detailVO.getImageUrl()));

         rowNum++;
         }


         return workbook;
     }

     */

    /**
     * 构建下载链接
     * @param wb
     * @param res
     * @param fileName 文件名
     * @throws IOException
     */
    public void exportToDownload(HSSFWorkbook wb, HttpServletResponse res, String fileName) throws IOException {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        wb.write(os);
        byte[] content = os.toByteArray();
        InputStream is = new ByteArrayInputStream(content);
        // 设置response参数，可以打开下载页面
        res.reset();
        res.setContentType("application/vnd.ms-excel;charset=utf-8");
        res.setHeader("Content-Disposition", "attachment;filename="
                + new String((fileName + ".xls").getBytes(), "iso-8859-1"));
        ServletOutputStream out = res.getOutputStream();
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;

        try {
            bis = new BufferedInputStream(is);
            bos = new BufferedOutputStream(out);
            byte[] buff = new byte[2048];
            int bytesRead;
            while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
                bos.write(buff, 0, bytesRead);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bis != null){
                bis.close();
            }
            if (bos != null){
                bos.close();
            }
        }
    }
}
